package com.rongxue.service.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;


import com.rongxue.service.fxq.JksCore;
import com.rongxue.service.fxq.certificate.ExportCertificate;
import com.rongxue.service.fxq.certificate.RequireCACertificate;
import com.rongxue.service.fxq.certificate.require.CACertificateRequire;
import com.rongxue.service.fxq.certificate.require.ExportCertificateRequire;
import com.rongxue.service.fxq.container.GenerateJksContainer;
import com.rongxue.service.fxq.container.require.GenerateJksContainerRequire;
import com.rongxue.service.fxq.signature.SignPdf;
import com.rongxue.service.fxq.signature.require.SignPdfRequire;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;

import java.io.File;
import java.util.Map;

/**
 * Created by Administrator on 2018/7/30.
 */
public class FxqUtils {
    public static String jksSavePath = "/program/apache-tomcat-7.0.39/webapps/pdf";

//    public static String jksSavePath ="D:/pdf";

    public static final String keyStoreName ="jishang.ks";//容器名

    public static final String keyStorePd ="jishang@666";//容器密码

    public static final String fcAlias ="jishang-cert";//第一张证书的别名

    public static final String fcPassword ="jishang@111";//第一张证书的密码

    public static final String fcName ="jsrongda";//容器名

    public static final String StoreName ="jishang.ks";//容器名

    private static final Logger LOGGER = LoggerFactory.getLogger(FxqUtils.class);


    /**
     * you can use
     * keytool -list -v -keystore ./demo.ks
     * to view keystore detail
     */
    //生成容器
    static {
        JksCore jksCore = new JksCore();
        GenerateJksContainer generateJksContainer = new GenerateJksContainer();
        GenerateJksContainerRequire generateJksContainerRequire = new GenerateJksContainerRequire();
        generateJksContainerRequire.setSavePath(jksSavePath)
                .setKeyStoreName(keyStoreName)
                .setKeyStorePd(keyStorePd)//container password
                .setFcAlias(fcAlias)//alias of first certificate
                .setFcPassword(fcPassword)//password of first certificate
                .setFcName(fcName)
                .setFcCountry("CN")
                .setFcProvince("SH")
                .setFcCity("Shanghai");
        JSONObject result = jksCore.doJob(generateJksContainer, generateJksContainerRequire);
        Assert.assertEquals("success", result.get("msg"));


//        JksCore jksCore2 = new JksCore();
//        GenerateCertificate generateCertificate = new GenerateCertificate();
//        GenerateCertificateRequire generateCertificateRequire = new GenerateCertificateRequire();
//        generateCertificateRequire.setAlias(fcAlias)
//                .setCertPassword(fcPassword)
//                .setCountry("CN")
//                .setProvince("SH")
//                .setCity("Shanghai")
//                .setName("jishang")
//                .setKeystorePath(jksSavePath)
//                .setKeystoreName(keyStoreName)
//                .setStorepass(keyStorePd)
//                .setValidity(1);
//        JSONObject result2 = jksCore2.doJob(generateCertificate, generateCertificateRequire);
//        Assert.assertEquals("success", result2.get("msg"));

        JksCore jksCore2 = new JksCore();
        RequireCACertificate requireCACertificate = new RequireCACertificate();
        CACertificateRequire caCertificateRequire = new CACertificateRequire();
        caCertificateRequire.setAppId("d032b434a05f4ce4a15707d4d99cc205")
                .setAppSecret("916c6f538a894643a75ab30830112fe19b63f20e4f28451781c2248bbefced5d")
                .setHost("http://sdk.fangxinqian.cn:3030")
                .setPath("/cert/generate")
                .setName("limou")
                .setIdentityCode("123123123123123123")
                .setEmail("liumapp.com@gmail.com")
                .setOrganization("积商(上海)金融信息服务有限公司")
                .setOrganizationUnit("运营部")
                .setCertAlias("ca-cert-alias2")
                .setCertPassword(fcPassword)
                .setKeystoreName(keyStoreName)
                .setStorepass(keyStorePd)
                .setKeystorePath(jksSavePath);
        JSONObject result2 = jksCore2.doJob(requireCACertificate, caCertificateRequire);
        Assert.assertEquals("success", result.get("msg"));
    }


    public static void setUp() {
        File file = new File(jksSavePath);
        Assert.assertEquals(true, file.isDirectory());
//        super.setUp();
}

//    /**
//     * you can use
//     * keytool -list -v -keystore ./demo.ks
//     * to view keystore detail
//     */
//    //生成容器
//
//    public static void GenerateContainer (Map<String,String> map) {
//        JksCore jksCore = new JksCore();
//        GenerateJksContainer generateJksContainer = new GenerateJksContainer();
//        GenerateJksContainerRequire generateJksContainerRequire = new GenerateJksContainerRequire();
//        generateJksContainerRequire.setSavePath(jksSavePath)
//                .setKeyStoreName("jishang.ks")
//                .setKeyStorePd("jishang@365")//container password
//                .setFcAlias("jishang-cert")//alias of first certificate
//                .setFcPassword(map)//password of first certificate
//                .setFcName("liumapp")
//                .setFcCountry("CN")
//                .setFcProvince("ZJ")
//                .setFcCity("Hangzhou");
//        JSONObject result = jksCore.doJob(generateJksContainer, generateJksContainerRequire);
//        Assert.assertEquals("success", result.get("msg"));
//    }

    //生成证书
    public static boolean GenerateCertificate (Map<String,String> map) {
//        try {
//            JksCore jksCore = new JksCore();
//            GenerateCertificate generateCertificate = new GenerateCertificate();
//            GenerateCertificateRequire generateCertificateRequire = new GenerateCertificateRequire();
//            generateCertificateRequire.setAlias(map.get("alias"))
//                    .setCertPassword(fcPassword)
//                    .setCountry("CN")
//                    .setProvince(map.get("province"))
//                    .setCity(map.get("city"))
//                    .setName(map.get("name"))
//                    .setKeystorePath(jksSavePath)
//                    .setKeystoreName(keyStoreName)
//                    .setStorepass(keyStorePd)
//                    .setValidity(1);
//            JSONObject result = jksCore.doJob(generateCertificate, generateCertificateRequire);
//            Assert.assertEquals("success", result.get("msg"));
//        }catch (RuntimeException e){
//            e.printStackTrace();
//            return false;
//        }
//        return true;
        try {
            JksCore jksCore = new JksCore();
            RequireCACertificate requireCACertificate = new RequireCACertificate();
            CACertificateRequire caCertificateRequire = new CACertificateRequire();
            caCertificateRequire.setAppId("d032b434a05f4ce4a15707d4d99cc205")
                    .setAppSecret("916c6f538a894643a75ab30830112fe19b63f20e4f28451781c2248bbefced5d")
                    .setHost("http://sdk.fangxinqian.cn:3030")
                    .setPath("/cert/generate")
                    .setName(map.get("name"))
                    .setIdentityCode(map.get("idcard"))
                    .setEmail("service@jsrongdai.cn")
                    .setOrganization("积商(上海)金融信息服务有限公司")
                    .setOrganizationUnit("运营部")
                    .setCertAlias(map.get("alias"))
                    .setCertPassword(fcPassword)
                    .setKeystoreName(keyStoreName)
                    .setStorepass(keyStorePd)
                    .setKeystorePath(jksSavePath);
            JSONObject result = jksCore.doJob(requireCACertificate, caCertificateRequire);
            Assert.assertEquals("success", result.get("msg"));
        }catch (RuntimeException e){
            e.printStackTrace();
            return false;
        }
        return true;
    }


    public static boolean RequireCACertificate () {
        // todo
        try {
            JksCore jksCore = new JksCore();
            RequireCACertificate requireCACertificate = new RequireCACertificate();
            CACertificateRequire caCertificateRequire = new CACertificateRequire();
            caCertificateRequire.setAppId("d032b434a05f4ce4a15707d4d99cc205")
                    .setAppSecret("916c6f538a894643a75ab30830112fe19b63f20e4f28451781c2248bbefced5d")
                    .setHost("http://sdk.fangxinqian.cn:3030")
                    .setPath("/cert/generate")
                    .setName("limou")
                    .setIdentityCode("123123123123123123")
                    .setEmail("liumapp.com@gmail.com")
                    .setOrganization("积商(上海)金融信息服务有限公司")
                    .setOrganizationUnit("运营部")
                    .setCertAlias("ca-cert-alias2")
                    .setCertPassword(fcPassword)
                    .setKeystoreName(keyStoreName)
                    .setStorepass(keyStorePd)
                    .setKeystorePath(jksSavePath);
            JSONObject result = jksCore.doJob(requireCACertificate, caCertificateRequire);
            Assert.assertEquals("success", result.get("msg"));
        }catch (RuntimeException e){
            e.printStackTrace();
            return false;
        }
        return true;
    }


    //导出证书
    public void ExportCertificate () {
        JksCore jksCore = new JksCore();
        ExportCertificate exportCertificate = new ExportCertificate();
        ExportCertificateRequire exportCertificateRequire = new ExportCertificateRequire();
        exportCertificateRequire.setAlias("alias")
                .setCertSavePath(this.jksSavePath)
                .setCertName("first-cert.cer")
                .setKeystorePath(this.jksSavePath)
                .setKeystoreName("demo.ks")
                .setKeystorePasswd("123456");
        JSONObject result = jksCore.doJob(exportCertificate, exportCertificateRequire);
        Assert.assertEquals("success", result.get("msg"));
    }

    /**
     * todo
     */
//    @Test
//    @Ignore
//    public void testAddSignatureArea () {
//        JksCore jksCore = new JksCore();
//        AddSignatureArea addSignatureArea = new AddSignatureArea();
//        AddSignatureAreaRequire addSignatureAreaRequire = new AddSignatureAreaRequire();
//        addSignatureAreaRequire.setFirstX(100);
//        addSignatureAreaRequire.setFirstY(100);
//        addSignatureAreaRequire.setSecondX(250);
//        addSignatureAreaRequire.setSecondY(250);
//        addSignatureAreaRequire.setLocation("this is location value");
//        addSignatureAreaRequire.setReason("this is reason value");
//        addSignatureAreaRequire.setPageNum(1);
//        addSignatureAreaRequire.setPdfSavePath(this.jksSavePath);
//        addSignatureAreaRequire.setPdfFileName("test.pdf");
//        addSignatureAreaRequire.setResultSavePath(this.jksSavePath);
//        addSignatureAreaRequire.setResultSaveName("test-with-signature.pdf");
//        addSignatureAreaRequire.setSignFieldName("signature");
//        JSONObject result = jksCore.doJob(addSignatureArea, addSignatureAreaRequire);
//        Assert.assertEquals("success", result.get("msg"));
//    }

    /**
     * todo
     */
//    @Test
//    @Ignore
//    public void testAddMultySignatureArea () {
//        Integer number = 3;
//        JksCore jksCore = new JksCore();
//        AddSignatureArea addSignatureArea = new AddSignatureArea();
//        AddSignatureAreaRequire addSignatureAreaRequire = new AddSignatureAreaRequire();
//        for (int i = 0 ; i < number ; i++) {
//            JSONObject result = jksCore.doJob(addSignatureArea, addSignatureAreaRequire);
//            Assert.assertEquals("success", result.get("msg"));
//        }
//    }

    //签署第一个证书到pdf
    public static boolean testSignFirstCertificateToPdf (Map<String,String> map) {
        try {
            JksCore jksCore = new JksCore();
            SignPdf signPdf = new SignPdf();
            SignPdfRequire signPdfRequire = new SignPdfRequire();
            signPdfRequire.setKsPath(jksSavePath)
                    .setKsName(keyStoreName)
                    .setKsPassword(keyStorePd.toCharArray())
                    .setCertAlias(map.get("alias"))
                    .setCertPassword(fcPassword.toCharArray())
                    .setPdfSavePath(jksSavePath)
                    .setPdfFileName("template.pdf")
                    .setResultSavePath(jksSavePath)
                    .setResultSaveName(map.get("saveName"))
                    .setReason("")
                    .setLocation("")
                    .setFirstX(110)
                    .setFirstY(650)
                    .setSecondX(190)
                    .setSecondY(730)
                    .setPageNum(11)
                    .setSignFieldName(map.get("fieldName"))
                    .setSignPicPath(map.get("signPicPath"));

            LOGGER.info("signPdfRequire:"+ JSON.toJSONString(signPdfRequire));
            JSONObject result = jksCore.doJob(signPdf, signPdfRequire);
            Assert.assertEquals("success", result.get("msg"));
        }catch (RuntimeException e){
            e.printStackTrace();
            return false;
        }
    return true;
    }

    //签署第二个证书到pdf
    public static boolean testSignSecondCertificateToPdf (Map<String,String> map) {
        try {
            JksCore jksCore = new JksCore();
            SignPdf signPdf = new SignPdf();
            SignPdfRequire signPdfRequire = new SignPdfRequire();
            signPdfRequire.setKsPath(jksSavePath)
                    .setKsName(keyStoreName)
                    .setKsPassword(keyStorePd.toCharArray())
                    .setCertAlias(map.get("alias"))
                    .setCertPassword(fcPassword.toCharArray())
                    .setPdfSavePath(jksSavePath)
                    .setPdfFileName(map.get("pdfFileName"))
                    .setResultSavePath(jksSavePath)
                    .setResultSaveName(map.get("saveName"))
                    .setReason("")
                    .setLocation("")
                    .setFirstX(110)
                    .setFirstY(570)
                    .setSecondX(190)
                    .setSecondY(650)
                    .setPageNum(11)
                    .setSignFieldName(map.get("fieldName"))
                    .setSignPicPath(map.get("signPicPath"));
            JSONObject result = jksCore.doJob(signPdf, signPdfRequire);
            Assert.assertEquals("success", result.get("msg"));
        }catch (RuntimeException e){
            e.printStackTrace();
            return  false;
        }
        return true;
    }

}
